# coding:utf-8
import csv
import json

csv_header = ['cus_num',
              'request_key',
              'request_key_type',
              'flag_key',
              'start_key',
              'start_type',
              'start_name_firstTime',
              'start_name_lastTime',
              'start_name_relation_validate',
              'start_name_value',
              'start_home_addr_firstTime',
              'start_home_addr_lastTime',
              'start_home_addr_value',
              'start_person_addr_firstTime',
              'start_person_addr_lastTime',
              'start_person_addr_value',
              'start_school_addr_firstTime',
              'start_school_addr_lastTime',
              'start_school_addr_value',
              'start_biz_addr_firstTime',
              'start_biz_addr_lastTime',
              'start_biz_addr_value',
              'start_biz_tel_firstTime',
              'start_biz_tel_lastTime',
              'start_biz_tel_value',
              'start_home_tel_firstTime',
              'start_home_tel_lastTime',
              'start_home_tel_value',
              'start_bank_card_firstTime',
              'start_bank_card_lastTime',
              'start_bank_card_relation_validate',
              'start_bank_card_value',
              'start_qq_firstTime',
              'start_qq_lastTime',
              'start_qq_value',
              'start_marry_firstTime',
              'start_marry_lastTime',
              'start_marry_value',
              'start_education_firstTime',
              'start_education_lastTime',
              'start_education_value',
              'start_school_major_firstTime',
              'start_school_major_lastTime',
              'start_school_major_value',
              'start_company_name_firstTime',
              'start_company_name_lastTime',
              'start_company_name_value',
              'start_car_firstTime',
              'start_car_lastTime',
              'start_car_value',
              'start_is_card_firstTime',
              'start_is_card_lastTime',
              'start_is_card_value',
              'start_is_black_firstTime',
              'start_is_black_lastTime',
              'start_is_black_value',
              'start_is_bomb_firstTime',
              'start_is_bomb_lastTime',
              'start_is_bomb_value',
              'end_key',
              'end_type',
              'end_name_firstTime',
              'end_name_lastTime',
              'end_name_relation_validate',
              'end_name_value',
              'end_home_addr_firstTime',
              'end_home_addr_lastTime',
              'end_home_addr_value',
              'end_person_addr_firstTime',
              'end_person_addr_lastTime',
              'end_person_addr_value',
              'end_school_addr_firstTime',
              'end_school_addr_lastTime',
              'end_school_addr_value',
              'end_biz_addr_firstTime',
              'end_biz_addr_lastTime',
              'end_biz_addr_value',
              'end_biz_tel_firstTime',
              'end_biz_tel_lastTime',
              'end_biz_tel_value',
              'end_home_tel_firstTime',
              'end_home_tel_lastTime',
              'end_home_tel_value',
              'end_bank_card_firstTime',
              'end_bank_card_lastTime',
              'end_bank_card_relation_validate',
              'end_bank_card_value',
              'end_qq_firstTime',
              'end_qq_lastTime',
              'end_qq_value',
              'end_marry_firstTime',
              'end_marry_lastTime',
              'end_marry_value',
              'end_education_firstTime',
              'end_education_lastTime',
              'end_education_value',
              'end_school_major_firstTime',
              'end_school_major_lastTime',
              'end_school_major_value',
              'end_company_name_firstTime',
              'end_company_name_lastTime',
              'end_company_name_value',
              'end_car_firstTime',
              'end_car_lastTime',
              'end_car_value',
              'end_is_card_firstTime',
              'end_is_card_lastTime',
              'end_is_card_value',
              'end_is_black_firstTime',
              'end_is_black_lastTime',
              'end_is_black_value',
              'end_is_bomb_firstTime',
              'end_is_bomb_lastTime',
              'end_is_bomb_value',
              'edge_depth',
              'edge_relation_type',
              'edge_relation_validate_firstTime',
              'edge_relation_validate_lastTime',
              'edge_relation_validate_value',
              'edge_relation_sources',
              'edge_apicodes',
              'edge_apicode_COMP_ID',
              'edge_apicode_LICENSE_TYPE',
              'edge_apicode_SUBLICENSE_TYPE',
              'edge_apicode_LICENSE_CATEGORY',
              'edge_apicode_ISSUING_AUTHORITY',
              'edge_apicode_CUST_MARK',
              'edge_apicode_BUSINESS_SCENE',
              'edge_apicode_SUB_BUSINESS_SCENE',
              'edge_apicode_RISK_APPETITE',
              'edge_apicode_LOAN_CYCLE',
              'edge_apicode_CREDIT_LOWER_LINE',
              'edge_apicode_CREDIT_UPPER_LINE',
              'edge_apicode_INCOMING_CHANNEL',
              'edge_apicode_LOAN_TYPE',
              'edge_apicode_CUST_DEPT',
              'edge_apicode_IS_SOME_WAY',
              'edge_source_date_ac_nums']


def propArr(value, propKey):
    result = []
    for tmp in value:
        tmp_value = tmp[propKey].encode("utf-8")
        tmp_value_str = str(tmp_value, encoding='utf-8')
        result.append(tmp_value_str)
    resultStr = json.dumps(result, ensure_ascii=False)
    resultStr = resultStr.replace(',', '，')
    return resultStr


def is_exist(prop, jsonData):
    if prop in jsonData:
        return True
    else:
        return False


def fillPropByValidate(propKey, properties):
    result = []
    if is_exist(propKey, properties):
        name = properties[propKey]
        result.append(propArr(name, 'firstTime'))
        result.append(propArr(name, 'lastTime'))
        result.append(propArr(name, 'relation_validate'))
        result.append(propArr(name, 'value'))
    else:
        result.append('')
        result.append('')
        result.append('')
        result.append('')
    return result


def fillProp(propKey, properties):
    result = []
    if is_exist(propKey, properties):
        name = properties[propKey]
        result.append(propArr(name, 'firstTime'))
        result.append(propArr(name, 'lastTime'))
        result.append(propArr(name, 'value'))
    else:
        result.append('')
        result.append('')
        result.append('')
    return result


def start_or_end_deal(value):
    result = []
    result.append(value["key"])
    result.append(value["type"])
    properties = find_prop("properties", value)

    result.extend(fillPropByValidate("name", properties))
    result.extend(fillProp("home_addr", properties))
    result.extend(fillProp("person_addr", properties))
    result.extend(fillProp("school_addr", properties))
    result.extend(fillProp("biz_addr", properties))
    result.extend(fillProp("biz_tel", properties))
    result.extend(fillProp("home_tel", properties))
    result.extend(fillPropByValidate("bank_card", properties))
    result.extend(fillProp("qq", properties))
    result.extend(fillProp("marry", properties))
    result.extend(fillProp("education", properties))
    result.extend(fillProp("school_major", properties))
    result.extend(fillProp("company_name", properties))
    result.extend(fillProp("car", properties))
    result.extend(fillProp("is_card", properties))
    result.extend(fillProp("is_black", properties))
    result.extend(fillProp("is_bomb", properties))
    return result


def append_empty(size, result):
    for i in range(1, size):
        result.append("")


def relation_sources_deal(value):
    result = []
    items = value.items()
    for key, value in items:
        result.append(str(key))
    resultStr = json.dumps(result, ensure_ascii=False)
    resultStr = resultStr.replace(',', '，')
    return resultStr


api_code_empty = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']


def find_prop(prop, jsonData):
    if is_exist(prop, jsonData):
        return jsonData[prop]
    else:
        return ""


# 处理apicode
def apiCodeDeal(value, result):
    api_code_result = []
    COMP_ID_result = []
    LICENSE_TYPE_result = []
    SUBLICENSE_TYPE_result = []
    LICENSE_CATEGORY_result = []
    ISSUING_AUTHORITY_result = []
    CUST_MARK_result = []
    BUSINESS_SCENE_result = []
    SUB_BUSINESS_SCENE_result = []
    RISK_APPETITE_result = []
    LOAN_CYCLE_result = []
    CREDIT_LOWER_LINE_result = []
    CREDIT_UPPER_LINE_result = []
    INCOMING_CHANNEL_result = []
    LOAN_TYPE_result = []
    CUST_DEPT_result = []
    IS_SOME_WAY_result = []
    numsResults = []
    for prod_key in value:
        months = value[prod_key]
        if not isinstance(months, dict):
            result.extend(api_code_empty)
            return result
        for month_key in months:
            days = months[month_key]
            if not isinstance(days, dict):
                result.extend(api_code_empty)
                return result
            for day_key in days:
                day = days[day_key]
                if not isinstance(day, dict):
                    result.extend(api_code_empty)
                    return result
                for ac_key in day:
                    ac = day[ac_key]
                    if not isinstance(ac, dict):
                        result.extend(api_code_empty)
                        return result
                    for api_code_key in ac:
                        api_code_str = str(api_code_key)
                        api_code = ac[api_code_str]
                        if api_code_str not in api_code_result:
                            api_code_result.append(api_code_str)
                            COMP_ID_result.append(find_prop("COMP_ID", api_code))
                            LICENSE_TYPE_result.append(find_prop("LICENSE_TYPE", api_code))
                            SUBLICENSE_TYPE_result.append(find_prop("SUBLICENSE_TYPE", api_code))
                            LICENSE_CATEGORY_result.append(find_prop("LICENSE_CATEGORY", api_code))
                            ISSUING_AUTHORITY_result.append(find_prop("ISSUING_AUTHORITY", api_code))
                            CUST_MARK_result.append(find_prop("CUST_MARK", api_code))
                            BUSINESS_SCENE_result.append(find_prop("BUSINESS_SCENE", api_code))
                            SUB_BUSINESS_SCENE_result.append(find_prop("SUB_BUSINESS_SCENE", api_code))
                            RISK_APPETITE_result.append(find_prop("RISK_APPETITE", api_code))
                            LOAN_CYCLE_result.append(find_prop("LOAN_CYCLE", api_code))
                            CREDIT_LOWER_LINE_result.append(find_prop("CREDIT_LOWER_LINE", api_code))
                            CREDIT_UPPER_LINE_result.append(find_prop("CREDIT_UPPER_LINE", api_code))
                            INCOMING_CHANNEL_result.append(find_prop("INCOMING_CHANNEL", api_code))
                            LOAN_TYPE_result.append(find_prop("LOAN_TYPE", api_code))
                            CUST_DEPT_result.append(find_prop("CUST_DEPT", api_code))
                            IS_SOME_WAY_result.append(find_prop("IS_SOME_WAY", api_code))
                        numsResults.append([prod_key, day_key, api_code_key, find_prop("num", api_code)])
    result.append(obj2json(api_code_result))
    result.append(obj2json(COMP_ID_result))
    result.append(obj2json(LICENSE_TYPE_result))
    result.append(obj2json(SUBLICENSE_TYPE_result))
    result.append(obj2json(LICENSE_CATEGORY_result))
    result.append(obj2json(ISSUING_AUTHORITY_result))
    result.append(obj2json(CUST_MARK_result))
    result.append(obj2json(BUSINESS_SCENE_result))
    result.append(obj2json(SUB_BUSINESS_SCENE_result))
    result.append(obj2json(RISK_APPETITE_result))
    result.append(obj2json(LOAN_CYCLE_result))
    result.append(obj2json(CREDIT_LOWER_LINE_result))
    result.append(obj2json(CREDIT_UPPER_LINE_result))
    result.append(obj2json(INCOMING_CHANNEL_result))
    result.append(obj2json(LOAN_TYPE_result))
    result.append(obj2json(CUST_DEPT_result))
    result.append(obj2json(IS_SOME_WAY_result))
    result.append(obj2json(numsResults))


def obj2json(result):
    result_str = json.dumps(result, ensure_ascii=False)
    result_str = result_str.replace(',', '，')
    return result_str


def edge_deal(value):
    result = []
    result.append(find_prop("depth", value))
    result.append(find_prop("relation", value))
    properties = value["properties"]
    result.extend(fillProp("relation_validate", properties))
    source = properties["source"]
    result.append(relation_sources_deal(source))
    apiCodeDeal(source, result)
    return result


def getNonRepeatList(data):
    return [i for n, i in enumerate(data) if i not in data[:n]]


def json_deal(jsonStr, header_dict, row):
    str_tmp = jsonStr.replace('，', ',')
    jsonData = json.loads(str_tmp)
    items = jsonData.items()
    result = []
    for key, value in items:
        key_str = str(key)
        request_key_index = header_dict[key_str]
        flag = "h20_flag_" + key_str
        flag_key_index = header_dict[flag]
        cus_num_index = header_dict["cus_num"]

        item = []
        item.append(row[cus_num_index])

        item.append(row[request_key_index])

        item.append(key_str)

        item.append(row[flag_key_index])

        item.extend(start_or_end_deal(value["start"]))

        item.extend(start_or_end_deal(value["end"]))

        item.extend(edge_deal(value["edge"]))

        result.append(item)
    return result


def fill_flag_0_result(flag, request_key_type, row, header_dict, result):
    if flag == '0':
        cus_num_index = header_dict["cus_num"]
        request_key_index = header_dict[request_key_type]
        request_key = row[request_key_index]
        result_time = []
        result_time.append(row[cus_num_index])
        result_time.append(request_key)
        result_time.append(request_key_type)
        result_time.append(flag)
        result.append(result_time)


def read_excel(path):
    with open(path, mode="r", encoding='utf-8') as f:
        csv.field_size_limit(500 * 1024 * 1024)
        reader = csv.reader(f)
        # 表头索引字典
        dict = {}
        # 表头
        header_row_1 = next(reader)
        size = len(header_row_1)
        for idx, val in enumerate(header_row_1):
            val = val.encode('utf-8').decode('utf-8-sig')
            dict[val] = idx
        header_row_2 = next(reader)

        result = []
        for row in reader:
            # 处理flag为0的入参
            flag_0_deal(dict, result, row)
            # 处理flag为1的入参
            for idx, val in enumerate(row):
                if idx >= size:
                    arr = json_deal(val, dict, row)
                    print(arr)
                    result.extend(arr)
        return result


def flag_0_deal(dict, result, row):
    h20_flag_id_index = dict["h20_flag_id"]
    h20_flag_cell_index = dict["h20_flag_cell"]
    h20_flag_mail_index = dict["h20_flag_mail"]
    h20_flag_device_index = dict["h20_flag_device"]
    h20_flag_id = row[h20_flag_id_index]
    fill_flag_0_result(h20_flag_id, 'id', row, dict, result)
    h20_flag_cell = row[h20_flag_cell_index]
    fill_flag_0_result(h20_flag_cell, 'cell', row, dict, result)
    h20_flag_mail = row[h20_flag_mail_index]
    fill_flag_0_result(h20_flag_mail, 'mail', row, dict, result)
    h20_flag_device = row[h20_flag_device_index]
    fill_flag_0_result(h20_flag_device, 'device', row, dict, result)


def writer_excel(path, headers, rows):
    with open(path, mode="w", encoding='utf-8-sig', newline='') as f:
        f_csv = csv.writer(f)
        f_csv.writerow(headers)
        f_csv.writerows(rows)


def data_split(read_path, write_path):
    writer_excel(write_path, csv_header, read_excel(read_path))


data_split("C:\\Users\\zhang.cao\\Desktop\\新建文件夹\\图库建模小样本_res_56448.csv", "C:\\Users\\zhang.cao\\Desktop\\新建文件夹\\test.csv")
